<template>
  <div class="base-map-resource-content">
    <div class="query-content">
      <el-form
        :inline="true"
        :model="formInline"
        class="demo-form-inline backend-form"
      >
        <el-form-item label="底图名称">
          <el-input
            v-model="formInline.name"
            placeholder="请输入"
            @change="onSubmit"
          />
        </el-form-item>
        <!--        <el-form-item label="是否默认底图">-->
        <!--          <el-select v-model="formInline.region" placeholder="请选择">-->
        <!--            <el-option label="是" value="shanghai" />-->
        <!--            <el-option label="否" value="beijing" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item>-->
        <!--          <el-button type="primary" class="queryButton" @click="onSubmit"-->
        <!--            >查询</el-button-->
        <!--          >-->
        <!--          <el-button type="primary" class="resetButton" @click="onReset"-->
        <!--            >重置</el-button-->
        <!--          >-->
        <!--        </el-form-item>-->
        <el-form-item class="backend-button">
          <el-button
            class="addButton"
            type="primary"
            @click="handleAddClick"
            v-if="currentUser?.hasPermission('dtzygladd')"
          >
            <!--        <el-icon>-->
            <!--          <Plus />-->
            <!--        </el-icon>-->
            <i class="icon el-icon-plus"></i>
            {{ currentUser?.getButtonName('dtzygladd') }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-content">
      <div class="table">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column
            prop="mapName"
            align="center"
            label="底图名称"
            width="180"
          />
          <el-table-column label="缩略图" show-overflow-tooltip>
            <template #default="scope">
              <el-image
                style="height: 85px"
                v-if="scope.row.fileUrl"
                :src="getFilePath(scope.row.fileUrl)"
              >
                <template #error>
                  <div class="image-slot">
                    <div class="error-img">加载失败</div>
                  </div>
                </template>
              </el-image>
              <div class="error-img" v-else>暂无图片</div>
            </template>
          </el-table-column>
          <el-table-column label="是否默认底图" show-overflow-tooltip>
            <template #default="scope">
              <el-popconfirm
                width="220"
                confirm-button-text="确认"
                cancel-button-text="取消"
                icon-color="#626AEF"
                @confirm="
                  confirmEvent(
                    scope.row.pkid,
                    scope.row.isDefault,
                    scope.row.fileId
                  )
                "
                title="确认将当前选中底图设置为默认底图吗？"
              >
                <template #reference>
                  <el-switch :value="scope.row.isDefault === 1" />
                </template>
              </el-popconfirm>
              <!--              <span>{{ scope.row.isDefault == "1" ? "是" : "否" }}</span>-->
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="操作时间"
            show-overflow-tooltip
          />
          <el-table-column
            prop="updateTime"
            label="更新时间"
            show-overflow-tooltip
          />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button
                text
                type="text"
                style="padding: 0"
                @click="handleEditClick(scope.row)"
                v-if="currentUser?.hasPermission('dtzygledit')"
              >
                {{ currentUser?.getButtonName('dtzygledit') }}
              </el-button>
              <el-popconfirm
                width="160px"
                confirm-button-text="确认"
                cancel-button-text="取消"
                icon-color="#626AEF"
                @confirm="handleDeleteClick(scope.row)"
                title="确认删除该底图？"
              >
                <template #reference>
                  <el-button
                    text
                    type="text"
                    style="color: #fa3434; margin-left: 12px"
                    @click="handleDelete(scope.row)"
                    v-if="
                      currentUser?.hasPermission('dtzygldelete')
                    "
                  >
                    {{
                      currentUser?.getButtonName('dtzygldelete')
                    }}
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        background
        layout="slot,prev, pager, next"
        :page-size="formInline.pageSize"
        :current-page="formInline.pageIndex"
        :total="total"
        @current-change="handlePageChange"
      >
        <template> 共{{ total }}个底图</template>
      </el-pagination>
    </div>
    <add-base-map-dialog
      :visible="dialogVisible"
      :base-map-server-dict="serviceType"
      :basemap-info="baseMapInfo"
      @closeDialog="handleDialogClose"
      @updateBaseMap="handleBaseMapUpdate"
    >
    </add-base-map-dialog>
    <!--    <el-dialog-->
    <!--      :visible.sync="deleteVisible"-->
    <!--      @closed="handleDialogClose"-->
    <!--      title="提示"-->
    <!--      align-center-->
    <!--      class="outline-dialog"-->
    <!--      width="30%"-->
    <!--    >-->
    <!--      <span>确认删除该底图？</span>-->
    <!--      <template #footer>-->
    <!--        <div class="dialog-footer">-->
    <!--          <el-button @click="deleteVisible = false">取消</el-button>-->
    <!--          <el-button type="primary" @click="handleDeleteClick(baseMapInfo)">-->
    <!--            确认-->
    <!--          </el-button>-->
    <!--        </div>-->
    <!--      </template>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script setup lang="ts">
import { getCurrentInstance, onMounted, reactive, ref } from 'vue'
import { Message as ElMessage } from 'element-ui'
import AddBaseMapDialog from './components/addBaseMapDialog.vue'
import {
  deleteBaseMap,
  queryMapList,
  updateBaseMap,
} from '@/api/back-management/basemap'
import type { BaseMapDataResp } from '@/api/back-management/basemap'
import type { addBaseMapParams } from '@/api/back-management/basemap/interface'
import type { OptionData } from '@/utils/type'
// import { useCurrentUserStore } from '@/stores/current-user'
import { PERMISSIONS } from '@/common/user'
import { baseFileUrl } from '@/utils'

const getFilePath = (url: string) => {
  if (url) {
    return baseFileUrl + url
  } else {
    return ''
  }
}

const vm = getCurrentInstance()
const store = vm.proxy.$store

const currentUser = store.state.user.currentUser

// const { currentUser } = useCurrentUserStore()

const formInline = reactive({
  name: '',
  pageSize: 5,
  pageIndex: 1,
})

const dialogVisible = ref(false)
const deleteVisible = ref(false)
const total = ref(0)

const serviceType = ref<OptionData[]>([
  {
    label: 'WMS',
    value: 'wms',
  },
  {
    label: 'WMTS',
    value: 'wmts',
  },
])

const baseMapInfo = reactive<Partial<BaseMapDataResp>>({
  createTime: '',
  fileId: '',
  fileUrl: '',
  isDefault: 1,
  mapName: '',
  mapUrl: '',
  pkid: '',
  serviceType: '',
  updateTime: '',
})

const tableData = ref<BaseMapDataResp[]>([])
const getMapList = () => {
  queryMapList(formInline).then((res) => {
    if (res.success) {
      total.value = res.data.count
      tableData.value = res.data.list
    } else {
      ElMessage.error(res.message)
    }
  })
}

onMounted(() => {
  getMapList()
})

const handleAddClick = () => {
  dialogVisible.value = true
}

const onSubmit = () => {
  formInline.pageIndex = 1
  getMapList()
}

const onReset = () => {
  formInline.pageIndex = 1
  formInline.name = ''
  getMapList()
}

const handleEditClick = (item: BaseMapDataResp) => {
  Object.assign(baseMapInfo, item)
  dialogVisible.value = true
}

const handleDelete = (item: BaseMapDataResp) => {
  Object.assign(baseMapInfo, item)
  deleteVisible.value = true
}

const handleDeleteClick = (item: Partial<BaseMapDataResp>) => {
  deleteBaseMap(item.pkid!).then((res) => {
    if (res.success) {
      ElMessage.success('删除底图成功！')
      deleteVisible.value = false
      onReset()
    } else {
      ElMessage.error(res.message)
    }
  })
}

const handlePageChange = (page: number) => {
  formInline.pageIndex = page
  getMapList()
}

const handleDialogClose = () => {
  dialogVisible.value = false
  Object.assign(baseMapInfo, {
    createTime: '',
    fileId: '',
    fileUrl: '',
    isDefault: 1,
    mapName: '',
    mapUrl: '',
    pkid: '',
    serviceType: '',
    updateTime: '',
  })
}

const handleBaseMapUpdate = () => {
  dialogVisible.value = false
  onSubmit()
}

const confirmEvent = (pkid: string, status: 0 | 1, fileId: string) => {
  let params
  if (status === 1) {
    params = {
      isDefault: 0,
      fileId,
    } as addBaseMapParams
  } else {
    params = {
      isDefault: 1,
      fileId,
    } as addBaseMapParams
  }
  updateBaseMap(pkid, params).then((res) => {
    if (res.success) {
      onSubmit()
      ElMessage.success('更新底图成功！')
    } else {
      ElMessage.error(res.message)
    }
  })
}
</script>

<style lang="scss" scoped>
.base-map-resource-content {
  width: 100%;
  height: calc(100% - 27px);
  background-color: white;
  box-shadow: 0 0 6px 0 rgba(0, 0, 0, 0.1);
  border-radius: 2px;

  .query-content {
    padding-left: 20px;
    width: 100%;
    height: 80px;
    line-height: 80px;
    border-bottom: 1px solid #eeeff5;

    :deep(.el-form) {
      .el-form-item {
        margin-bottom: 0;

        .resetButton {
          width: 80px;
          height: 32px;
          color: #28b1e4;
          background: #ffffff;
          border: 1px solid #28b1e4;
          border-radius: 2px;
        }

        .queryButton {
          width: 80px;
          height: 32px;
          background: #28b1e4;
          border-radius: 2px;
        }
      }
    }
  }
  .addButton {
    margin-left: 20px;
    margin-top: 14px;
    margin-bottom: 14px;
    width: 100px;
    height: 32px;
    //background: #28b1e4;
    border-radius: 2px;
    //border: none;
    padding: 0;
  }
  .table-content {
    width: 100%;
    height: calc(100% - 63px);

    .table {
      width: 100%;
      padding: 0 18px;
      height: calc(100% - 77px - 32px - 28px);
      overflow-y: auto;

      :deep(.el-table__row--striped) {
        background: #f7f9fb;
      }

      .el-image {
        width: 85px;
        height: 85px;
      }

      :deep(.error-img) {
        width: 85px;
        height: 85px;
        font-size: 12px;
        color: #97a2b5;
        background-color: #fafcff;
        border: 1px solid #e1e9f4;
        border-radius: 2px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }

    .el-pagination {
      margin-top: 15px;
      justify-content: end;
      margin-right: 18px;
    }
  }
}
</style>
